package top.yokey.android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.io.File;
import java.util.List;

import top.yokey.android.R;
import top.yokey.android.activity.ModifyPasswordActivity;
import top.yokey.android.base.App;
import top.yokey.android.base.GlideEngine;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.HttpHelp;

public class MineFragment extends Fragment {

    private AppCompatImageView avatarImageView;
    private AppCompatTextView nicknameTextView;
    private AppCompatTextView idTextView;

    private AppCompatTextView modifyPasswordTextView;
    private AppCompatTextView logoutTextView;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_mine, container, false);
        avatarImageView = root.findViewById(R.id.avatarImageView);
        nicknameTextView = root.findViewById(R.id.nicknameTextView);
        idTextView = root.findViewById(R.id.idTextView);
        modifyPasswordTextView = root.findViewById(R.id.modifyPasswordTextView);
        logoutTextView = root.findViewById(R.id.logoutTextView);
        initEvent();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        nicknameTextView.setText(App.get().getMemberData().getMemberNickname());
        idTextView.setText("手机号：");
        idTextView.append(App.get().getMemberData().getMemberMobile());
        //Glide加载网络图片示例
        System.out.println(App.get().getMemberData().getMemberAvatar());
        Glide.with(this).load(App.get().getMemberData().getMemberAvatar()).into(avatarImageView);
    }

    private void initEvent() {

        avatarImageView.setOnClickListener(view -> {
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(List<LocalMedia> result) {
                            System.out.println(result.get(0).getPath());
                            // 结果回调
                            HttpHelp.get().ready("member", "modifyAvatar")
                                    .post(new File(result.get(0).getPath()), new BaseCallBack() {
                                        @Override
                                        public void onSuccess(String data) {
                                            App.get().getMemberData().setMemberAvatar(data);
                                            Glide.with(MineFragment.this).load(data).into(avatarImageView);
                                        }

                                        @Override
                                        public void onFailure(String reason) {

                                        }
                                    });
                        }

                        @Override
                        public void onCancel() {

                        }
                    });
        });

        modifyPasswordTextView.setOnClickListener(view -> startActivity(new Intent(getActivity(), ModifyPasswordActivity.class)));

        logoutTextView.setOnClickListener(view -> {
            new AlertDialog.Builder(getActivity())
                    .setTitle("确认您的选择~")
                    .setMessage("注销登录？")
                    .setCancelable(false)//此属性确定是否可以点击其他区域关闭对话
                    .setPositiveButton("确定", (dialogInterface, i) -> {
                        logoutTextView.setEnabled(false);
                        logoutTextView.setText("正在注销...");
                        HttpHelp.get().ready("member", "logout")
                                .get(new BaseCallBack() {
                                    @Override
                                    public void onSuccess(String data) {
                                        Toast.makeText(getActivity(), "注销成功", Toast.LENGTH_SHORT).show();
                                        //注销成功之后本地客户端的操作
                                        //清除掉本地存储的TOKEN
                                        App.get().getEditor().putString("token", "");
                                        App.get().getEditor().commit();
                                        //发送一个键名logout内容为success的消息
                                        LiveEventBus.get("logout").post("success");
                                    }

                                    @Override
                                    public void onFailure(String reason) {
                                        Toast.makeText(getActivity(), reason, Toast.LENGTH_SHORT).show();
                                        logoutTextView.setEnabled(true);
                                        logoutTextView.setText("注销登录");
                                    }
                                });
                    })
                    .setNegativeButton("取消", (dialogInterface, i) -> {

                    })
                    .show();
        });

    }

}
